package org.jiajie.coffeecodes.concurrent.lock;

import java.util.concurrent.Semaphore;

/**
 * @author jie
 * @date 2021/11/3 10:04 PM
 */
public class SemaTest {

    public static void main(String[] args) {
        // 构造函数入参，permits: 信号量；
        // fair：公平锁/非公平锁
        Semaphore semaphore = new Semaphore(2, false);

        for (int i = 0; i < 8; i++) {
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + " in the hole");
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {

                } finally {
                    semaphore.release();
                }
            }, "hole no. " + i).start();
        }
    }
}
